/**
 * Created by tanbingqin on 2020/3/11.
 */
import React, { Component, PropTypes } from "react";
import { connect } from 'react-redux';
import { Affix, Button, Modal, message } from 'antd'
import GoBackBtn from 'containers/request/btns/go-back-btn'
import SubmitBtn from 'containers/request/btns/submit-btn'
import GoLoanBtn from 'containers/request/btns/go-loan-btn'
import QuotaBtn from 'containers/request/btns/quota-btn'
import { messages } from "share/common";
import requestService from 'containers/request/request.service'
import moment from 'moment'
import errorMessage from 'share/errorMessage'

class BottomBlock extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deleteLoading: false,
    }
  }

  //删除单据
  handleDelete = () => {
    const { info, close } = this.context;
    Modal.confirm({
      title: messages('request-1.key259')/*删除单据*/,
      content: messages('request-1.key260')/*确定删除该单据？*/,
      onOk: () => {
        this.setState({deleteLoading: true});
        requestService.deleteRequest(info.applicationOID).then(res => {
          this.setState({deleteLoading: false});
          if (res.data.code === '70124' && res.data.rows && res.data.rows.length) {
            let isShowTime = res.data.toDateOverLap === '1003';
            let originStartDate = isShowTime ? moment(res.data.originStartDate).format('YYYY-MM-DD HH:mm') : moment(res.data.originStartDate).format('YYYY-MM-DD');
            let originEndDate = isShowTime ? moment(res.data.originEndDate).format('YYYY-MM-DD HH:mm') : moment(res.data.originEndDate).format('YYYY-MM-DD');
            let content = (
              <div style={{maxHeight: 400, overflowY: 'scroll'}}>
                <div>{messages('request-1.key261',{arg1: originStartDate,arg2: originEndDate})/*本单据上一版本中的开始结束日期（{arg1} ~ {arg2}）与已提交的单据开始结束日期重复：*/}</div>
                {res.data.rows.map(row => {
                  let rowStartDate = isShowTime ? moment(row.startDate).format('YYYY-MM-DD HH:mm') : moment(row.startDate).format('YYYY-MM-DD');
                  let rowEndDate = isShowTime ? moment(row.endDate).format('YYYY-MM-DD HH:mm') : moment(row.endDate).format('YYYY-MM-DD');
                  return (
                    <div>
                      <div>
                        {row.formName}{row.businessCode}，{messages('request-1.key122')/*申请人：*/}{row.applicantName}
                      </div>
                      <div>
                        {rowStartDate}~{rowEndDate}
                      </div>
                    </div>
                  )
                })}
              </div>
            );
            Modal.info({
              title: messages('request-1.key262')/*申请单无法删除*/,
              width: 800,
              content: content,
              okText: messages('common.ok')/*确定*/,
              onOk: () => {}
            });
          } else {
            message.success(messages('common.delete.success',{arg1: ''})/*{arg1} 删除成功*/);
            close(true);
          }
        }).catch(e => {
          this.setState({deleteLoading: false});
          errorMessage(e.response);
        });
      }
    });
  };

  render() {
    const { close } = this.context;
    const { deleteLoading } = this.state;
    return (
      <Affix offsetBottom={0} className="bottom-bar bottom-bar-edit">
        <QuotaBtn quotaObj={this.props.quotaObj} />
        <GoLoanBtn />
        <SubmitBtn quotaObj={this.props.quotaObj} />
        <Button className="delete-btn" loading={deleteLoading} onClick={this.handleDelete}>{messages('request-1.key259')/*删除单据*/}</Button>
        <GoBackBtn goBack={close} />
      </Affix>
    )
  }
}

BottomBlock.contextTypes = {
  applicant: PropTypes.object,
  creditScore: PropTypes.object,
  jobInfo: PropTypes.object,
  form: PropTypes.object,
  info: PropTypes.object,
  close: PropTypes.func,
};

function mapStateToProps(state) {
  return {
  }
}

export default connect(mapStateToProps)(BottomBlock)
